package lv.solcraft.replication.server.rest.restlets;

import java.util.List;

import lv.solcraft.replication.server.bdb.Database;
import lv.solcraft.replication.server.dao.OperSiteDataAccess;
import lv.solcraft.replication.server.model.OperSite;
import lv.solcraft.replication.server.model.OperState;
import lv.solcraft.replication.server.model.OperStateUpdate;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.restlet.Context;
import org.restlet.data.CharacterSet;
import org.restlet.data.Encoding;
import org.restlet.data.Form;
import org.restlet.data.Language;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;
import org.springframework.util.Assert;

import com.noelios.restlet.application.EncodeRepresentation;

public class OperSitesResource extends Resource {

    private Database database;
    private OperSiteDataAccess operSiteDataAccess;
    private int maxItems = 1000;
    private int skipItems = 0;

    public void setDatabase(Database database) {
        this.database = database;
    }

    @Override
    public void init(Context context, Request request, Response response) {
        super.init(context, request, response);
        long siteId = Long.parseLong(request.getAttributes().get("site").toString());
        Form query = request.getResourceRef().getQueryAsForm();
        maxItems = Integer.parseInt(query.getFirstValue("maxItems", "1000"));
        skipItems = Integer.parseInt(query.getFirstValue("skipItems", "0"));
        Assert.isTrue(maxItems - skipItems <= 100000, "maxItems - skipItems should be <= 100,000");
        OperState operState = OperState.valueOf(request.getAttributes().get("state").toString());
        operSiteDataAccess = database.getOperSiteDataAccessFactory().getInstance(siteId, operState);
        getVariants().add(new Variant(MediaType.TEXT_PLAIN));
    }

    @Override
    public boolean allowGet() {
        return true;
    }

    @Override
    public boolean allowDelete() {
        return true;
    }

    @Override
    public void delete() {
        List<OperSite> operSites = operSiteDataAccess.findBatch(skipItems, maxItems);
        for (OperSite operSite : operSites) {
            operSiteDataAccess.delete(operSite.getId());
        }
        getResponse().setEntity(new StringRepresentation(Integer.toString(operSites.size())));
    }

    @Override
    public Representation getRepresentation(Variant variant) {
        JSONObject root = new JSONObject();
        List<OperSite> operSites = operSiteDataAccess.findBatch(skipItems, maxItems);
        JSONArray operSitesJson = new JSONArray();
        for (OperSite operSite : operSites) {
            JSONObject operSiteJson = OperStateUpdate.fromOperSite(operSite).toJSON();
            JSONObject operJson = operSite.getOper().toJSON(false);
            JSONObject json = new JSONObject();
            json.put("operSite", operSiteJson);
            json.put("oper", operJson);
            operSitesJson.add(json);
        }
        root.put("rows", operSitesJson);
        StringRepresentation stringRepresentation = new StringRepresentation(root.toString(1), MediaType.TEXT_PLAIN, Language.ALL, CharacterSet.UTF_8);
        return new EncodeRepresentation(Encoding.GZIP, stringRepresentation);
    }
}
